package com.xingdata.jjxc.activity;

import java.util.HashMap;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.MessageCode;
import com.xingdata.jjxc.utils.NameLengthFilter;
import com.xingdata.jjxc.utils.SystemInfo;

public class Feedbackavt extends Base1Activity implements OnClickListener {

	private EditText input_feedback;
	private Button feedback_confirm;
	
	private String type = "feed";
	
	private Handler handler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		ExitApplication.getInstance().addActivity(this);
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_feedbackavt;
	}

	@Override
	protected String getTitleText() {
		return JUtils.TITILE_FEEDBACK;
	}

	@Override
	protected void initViews() {
		input_feedback = (EditText) findViewById(R.id.input_feedback_et);
		feedback_confirm = (Button) findViewById(R.id.feedback_confirm_btn);

		feedback_confirm.setOnClickListener(this);

		InputFilter[] filters = { new NameLengthFilter(this, 200,
				R.string.length_reg) };

		input_feedback.setFilters(filters);
		
		
		handler =new Handler(){
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case MessageCode.MessageCode_RefreshControl:
					// new MyCountTimer(180, 1000).start();
					//registerone_code.requestFocus();
					showToast(msg.getData().getString("result"));
					break;

				case MessageCode.MessageCode_ShowToast:
					showToast(msg.getData().getString("result"));
					break;

				default:
					break;
				}
			};
		};
	}
	
	
	
	@Override
	public void onClick(View v) {
		String feedback = input_feedback.getText().toString();
		switch (v.getId()) {
		case R.id.feedback_confirm_btn:
			if (feedback.length() == 0) {
				showToast("请评价我们的服务");
			} else {
				doPost_feedback(App.ZZD_REQUEST_FEEFBACK, feedback, type);
			}
			break;

		default:
			break;
		}

	}

	private void doPost_feedback(String url, String content, String type) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}

//
//		params.put("access_token",
//				Code.accessToken());
//		params.put("userid", SystemInfo.getUserInfo().getUserid());
//		params.put("feed_content", content);
//		params.put("type", type);
		paramsMap= new HashMap<String, String>();
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		paramsMap.put("feed_content", content);
		paramsMap.put("type", type);
		
		mHttpUtil.Post(url, paramsMap, new HttpResponseListener(){

			@Override
			public void onSuccess(RespEntity respEntity) {
				Bundle bundle = new Bundle();
				bundle.putString("result", respEntity.getMsg());
				Message message = new Message();
				message.setData(bundle);
				message.what = MessageCode.MessageCode_RefreshControl;
				handler.sendMessage(message);
				
			}

			@Override
			public void onFailure(String result) {
				Bundle bundle = new Bundle();
				bundle.putString("result", result);
				Message message = new Message();
				message.setData(bundle);
				message.what = MessageCode.MessageCode_ShowToast;
				handler.sendMessage(message);
				
			}

			@Override
			public void onLoading(int vurrent) {
				
			}
		});
		
//		mAbHttpUtil.post(url, params, new AbStringHttpResponseListener() {
//			@Override
//			public void onSuccess(int statusCode, String content) {
//				resp = JSON.parseObject(content, RespEntity.class);
//			}
//
//			@Override
//			public void onFailure(int statusCode, String content,
//					Throwable error) {
//			}
//
//			@Override
//			public void onStart() {
//				// 显示进度框
//				showProgressDialog();
//			}
//
//			@Override
//			public void onFinish() {
//				if (resp != null) {
//					if (resp.getState() == 0) {
//						showToast("感谢您的评论");
//						input_feedback.setText("");
//					} else if (resp.getState() == 1) {
//						showToast(resp.getMsg());
//					} else {
//						showToast(resp.getMsg());
//					}
//					removeProgressDialog();
//				} else {
//					showToast("网络超时，请重试");
//					removeProgressDialog();
//					return;
//				}
//			}
//		});
		finish();
	}

}
